package com.df.gameserver.net;

import com.df.script.IScript;
import com.df.gameserver.scriptmanager.MsgScriptManager;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MsgDispatcher {

    private enum Single {
        /**
         * 单例对象的实例
         */
        INSTANCE;
        private MsgDispatcher instance;

        Single() {
            this.instance = new MsgDispatcher();
        }
    }

    public static MsgDispatcher getInstance() {
        return MsgDispatcher.Single.INSTANCE.instance;
    }

    private MsgDispatcher() {}


    public void dispatchMsg(Channel channel, int msgId, Object msg){
        //消息分发 根据消息类型分发到不同的线程
        //将消息封装成命令 分发到不同线程池
        IScript script = MsgScriptManager.getInstance().getScript(msgId);
        //TODO 这里可以把user也传过去
        try {
            script.excute(channel,msg);
        }catch (Exception e){
            log.error("脚本执行错误，脚本id："+script.getScriptId());
            e.printStackTrace();
        }

    }
}
